<?php 
namespace Api\Controller;
use Think\Controller;

class LoginController extends Controller{
public function login(){
				$user=I('post.user');
				$pwd=I('post.pwd');
				if(empty($user) || empty($pwd)){
						$this->ajaxReturn(['code'=>3,'msg'=>'账号或密码为空']);die;	
					}
				
				$Model = M('login');
				$data = $Model->where(['CzyName'=>$user])->find();
				
				if(empty($data)){
						$this->ajaxReturn(array('code'=>4,'msg'=>'用户不存在'));die;

					}
					
				// $a='e10adc3949ba59abbe56e057f20f883e';//$data['CzyPassmd']
				$pwd=trim(strtoupper(md5($pwd)));
				
				if ($pwd!=$data['czypassmd']) {
					$this->ajaxReturn(array('code'=>5,'msg'=>'密码错误'));die;
				}

				//判断系统有没有手机权限
				$Model = M('config');
				$ConfigData = $Model->find();
				if ($ConfigData['sj']==0) {
					$this->ajaxReturn(array('code'=>6,'msg'=>'系统没有权限登陆'));die;
				}
				
				//判断用户有没有手机登陆权限
				if ($data['qxsj']==0) {
					$this->ajaxReturn(array('code'=>7,'msg'=>'没有权限登陆'));die;
				}
				//把登陆信息写入log日志
				$admin=M('log');
				$row = array(
						
						'czLr' => $data['czyname'].'成功登陆系统',
						'CzName' => $data['czyname'],
						'czdate' => date('Y-m-d H:i:s',time()),
						'gs'=>'jsapp',
						'yyyy'=>'fox'
					);
				$admin->add($row);
				//生成token缓存
				$token=MD5($user.$pwd.time());
				S($user.'token',$token,array('type'=>'file'));
				$this->ajaxReturn(['code'=>8,'msg'=>'登陆成功','data'=>['token'=>$token,'user'=>$user]]);die;


}

	public function changepwd(){
		$user=I('post.user');//用户名
		$pwd=I('post.pwd');//以前的密码
		$NewPwd=I('post.newpwd');//要修改的密码

		$Model=M('login');
		$log=M('log');
		$login=$Model->where(['CzyName'=>$user])->find();//用户信息

		
		if ($pwd!=$NewPwd) {
			$this->ajaxReturn(['code'=>102,'msg'=>'确认密码有误']);die;
		}
		// if (md5($pwd)!=$login['czypassmd']) {//验证输入的老密码是否正确
		// 	$this->ajaxReturn(['code'=>102,'msg'=>'原密码输入错误']);die;
		// }

		//新密码MD5加密，添加到数据库
		$NewPwd=trim(strtoupper(md5($pwd)));
		$data['CzyPassmd']=$NewPwd;

		$b=$Model->where(['CzyName'=>$user])->save($data);
		if ($b) {
			//操作写入日志
			$row = array(
						
						'czLr' => $login['czyname'].'成功修改了APP密码',
						'CzName' => $login['czyname'],
						'czdate' => date('Y-m-d H:i:s',time()),
						'gs'=>'jsapp',
						'yyyy'=>'fox'
					);
				$log->add($row);

			$CacheToken=S($TokenName,null);
			$this->ajaxReturn(['code'=>101,'msg'=>'修改成功']);die;
		}else{
			$this->ajaxReturn(['code'=>103,'msg'=>'修改失败']);die;
		}

		
	}

	public function loginout(){
		$CacheToken=S($TokenName,null);
		

	}


}










 ?>